package cn.stylefeng.guns.animals.modular.product.controller;

import cn.stylefeng.guns.animals.modular.product.service.IProductService;
import cn.stylefeng.guns.animals.modular.system.dao.ProductMapper;
import cn.stylefeng.guns.animals.modular.system.model.Product;
import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//  需要去掉包名 import cn.stylefeng.guns.animals.core.log.LogObjectHolder;

/**
 * 产品管理控制器
 *
 * @author fengshuonan
 * @Date 2020-08-26 17:01:27
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    private String PREFIX = "/product/product/";

    @Autowired
    private IProductService productService;

    /**
     * 跳转到产品管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "product.html";
    }

    /**
     * 跳转到添加产品管理
     */
    @RequestMapping("/product_add")
    public String productAdd() {
        return PREFIX + "product_add.html";
    }

    /**
     * 跳转到复制产品管理
     */
    @RequestMapping("/product_copy/{productId}")
    public String productCopy(@PathVariable Integer productId, Model model) {
        Product product = productService.selectById(productId);
        model.addAttribute("item", product);
        LogObjectHolder.me().set(product);
        return PREFIX + "product_copy.html";
    }

    /**
     * 跳转到修改产品管理
     */
    @RequestMapping("/product_update/{productId}")
    public String productUpdate(@PathVariable Integer productId, Model model) {
        Product product = productService.selectById(productId);
        model.addAttribute("item", product);
        LogObjectHolder.me().set(product);
        return PREFIX + "product_edit.html";
    }

    /**
     * 跳转到查看产品管理详情
     */
    @RequestMapping("/product_detail/{productId}")
    public String productDetail(@PathVariable Integer productId, Model model) {
        Product product = productService.selectById(productId);
        model.addAttribute("item",product);
        LogObjectHolder.me().set(product);
        return PREFIX + "product_detail.html";
    }

    /**
     * 获取产品管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Product product) {

        Page<Product> page = new PageFactory<Product>().defaultPage();
        List<Product> list = productService.queryPage(page, product);
        page.setRecords(list);
        return new PageInfoBT<>(page);
    }

//    @RequestMapping(value = "/list")
//    @ResponseBody
//    public Object list(String condition,String condition2) {
//
//        //按段
//        if (ToolUtil.isNotEmpty(condition)||ToolUtil.isNotEmpty(condition2)){
//
//            EntityWrapper<Product> productEntityWrapper = new EntityWrapper<>();
//            Wrapper<Product> spmc = productEntityWrapper.and().like("spmc", "%" + condition + "%" ).and().like("cgyhmc", "%" + condition2 + "%" );
//            List<Product> products = productMapper.selectList(spmc);
//            return products;
//        }else{
//            List<Product> product = productMapper.selectList(null);
//
//            return product;
//        }
//
//
//    }


    /**
     * 1
     * 新增产品管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Product product) {
        productService.insert(product);
        return SUCCESS_TIP;
    }

    /**
     * 1
     * 复制产品管理
     */
    @RequestMapping(value = "/copy")
    @ResponseBody
    public Object copy(Product product) {
        productService.insert(product);
        return SUCCESS_TIP;
    }

    /**
     * 删除产品管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer productId) {
        productService.deleteById(productId);
        return SUCCESS_TIP;
    }

    /**
     * 修改产品管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Product product) {

        productService.updateById(product);
        return SUCCESS_TIP;
    }


    /**
     * 产品管理详情
     */
    @RequestMapping(value = "/detail/{productId}")
    @ResponseBody
    public Object detail(@PathVariable("productId") Integer productId) {
        return productService.selectById(productId);
    }
}
